//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core.Serialization;
using OpenLS.Spreadsheet.Collections;
using OpenLS.Spreadsheet.Expressions;
using OpenLS.Spreadsheet.Extensibility;
using OpenLS.Spreadsheet.Office2003;

namespace OpenLS.Spreadsheet
{
    partial class Worksheet : IOffice2003XmlSerializable
    {
        #region IXmlSpreadsheetSerializable Members

        void IOffice2003XmlSerializable.ReadXmlSpreadsheet(ReadContext context)
        {
            TopLeftPane.DisableEvents();
            using (ReadContext c = context.Read("Worksheet"))
            {
                CellMerger cm = new CellMerger();
                c.AddData<CellMerger>(cm);
                Name = c.GetOptionalString(c.Namespace+ "Name");
                c.EatForNow("Names");
                if (c.ReaderLocalName == "Table")
                {
                    using (ReadContext cTable = c.Read("Table"))
                    {
                        ExpandedRange r = cTable.GetData<ExpandedRange>();
                        r.ExpandedColumnCount = cTable.GetOptionalInteger(XmlSpreadsheetConstants.XmlSpreadsheetNamespace+ "ExpandedColumnCount", 1);
                        r.ExpandedRowCount = cTable.GetOptionalInteger(XmlSpreadsheetConstants.XmlSpreadsheetNamespace + "ExpandedRowCount", 1);
                        IOffice2003XmlSerializable s = Columns;
                        s.ReadXmlSpreadsheet(cTable);
                        int availableIndex = 0;
                        while (cTable.ReaderLocalName == "Row")
                        {
                            Row row = new Row(this);
                            row.ReadXml2003(cTable, availableIndex);
                            Rows.Add(row);
                            foreach (Cell cell in row.GetNonEmptyCells())
                            {
                                NonVolatileCells.AddNoRow(cell);
                                cell.Column.Add(cell);
                            }
                            availableIndex = row.Index + 1;
                        }
                    }
                }
                c.EatForNow("WorksheetOptions");
                while (c.ReaderLocalName == "AutoFilter")
                    c.EatForNow("AutoFilter");
                c.EatForNow("PageBreaks");
                while (c.ReaderLocalName == "ConditionalFormatting")
                    c.EatForNow("ConditionalFormatting");
                while (c.ReaderLocalName == "QueryTable")
                    c.EatForNow("QueryTable");
                while (c.ReaderLocalName == "PivotTable")
                    c.EatForNow("PivotTable");
                while (c.ReaderLocalName == "DataValidation")
                    c.EatForNow("DataValidation");
                c.EatForNow("Watches");
                c.EatForNow("PublishObjects");
                cm.MergeWorksheet(this);
            }
            TopLeftPane.EnableEvents();


            
        }

        #endregion

        public ICell GetCell(int row, int column)
        {
            return this[row, column];
        }
    }
}
